<?php
namespace common\service;

use common\models\ContralSet;
use common\models\OrderStatusBackendLog;
use common\models\OrderStatusLog;
use common\models\SignDistanceLog;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * 业务常用类
 * Class BusinessService
 * @package common\servic
 * @author wuxin
 */
class BusinessService {

    /**
     * 剩下时间
     * @param $time
     * @return float
     */
    public static function getLeftTime($time){
        $leftS = strtotime($time)-strtotime('now') ;
        $leftM = floor($leftS/60) ;
        return $leftM ;
    }

    /**
     * 两个时间段相差的分钟数
     * @param $time1
     * @param $time2
     * @return float|string
     */
    public static function getDiffTime($time1=null,$time2=null){
        if(empty($time2)){
            return "" ;
        }
        if(empty($time1)){
            return "" ;
        }
        $leftS = strtotime($time2)-strtotime($time1) ;
        $leftM = floor($leftS/60) ;
        return $leftM ;
    }

    public static function getStartTime($order){
        $time = null ;
        if(!empty($order['receive_th_data_time'])){
            $time = $order['receive_th_data_time'] ;
        }
        if(!empty($order['receive_data_time'])){
            $time = $order['receive_data_time'] ;
        }
        if(!empty($order['receive_entity_time'])){
            $time = $order['receive_entity_time'] ;
        }
        if(!empty($order['dispatching_time'])){
            $time = $order['dispatching_time'] ;
        }
        if(!empty($order['sign_time'])){
            $time = $order['sign_time'] ;
        }
        return $time ;
    }

    /**
     * 获取年月日
     * @param $dateTime
     * @return bool|string
     */
    public static function getDate($dateTime){
        if(empty($dateTime)){
            return "" ;
        }
        return date("Y-m-d",strtotime($dateTime));
    }

    /**
     * 获取时分秒
     * @param $dateTime
     * @return bool|string
     */
    public static function getTime($dateTime){
        if(empty($dateTime)){
            return "" ;
        }
        return date("H:i:s",strtotime($dateTime));
    }

    /**
     * 订单状态修改日志
     * @param $logArr
     */
    public static function addOrderlog($logArr,$apiUser=false){
        $log = new OrderStatusLog() ;
        $log->orderno = $logArr['orderno'] ;
        $log->status = $logArr['status'] ;
        $log->sub_status = empty($logArr['sub_status'])?0:$logArr['sub_status'] ;

        if(Yii::$app->id=="app-api"){
//            $username = empty($logArr['username'])?"":$logArr['username'];
            if(empty($apiUser)){
                $username = "none" ;
                $phone = "" ;
                $user_id = null ;
            }else{
                $username = $apiUser->username ;
                $phone = $apiUser->phone ;
                $user_id = $apiUser->user_id ;
            }
        }else{
            if(Yii::$app->admin->isGuest){
                $username = "none" ;
                $phone = "" ;
                $user_id = null ;
            }else{
                $username = Yii::$app->admin->identity->username ;
                $phone = Yii::$app->admin->identity->phone ;
                $user_id = Yii::$app->admin->identity->user_id ;
            }
        }
        $log->opt_name = $username ;
        $log->opt_time = date("Y-m-d H:i:s") ;
        $log->remark = empty($logArr['remark'])?"":$logArr['remark'];
        $log->user_id = $user_id ;
        $log->phone = $phone ;

        $rs = $log->save() ;
        if(empty($rs)){
            $msg = current($log->getFirstErrors());
            return ["code"=>400,"msg"=>"失败,原因:".$msg];
        }
        return ["code"=>200,"msg"=>"成功"];
    }

    /**
     * 订单状态修改日志
     * @param $logArr
     */
    public static function addBackendOrderlog($logArr){
        $log = new OrderStatusBackendLog() ;
        $log->orderno = $logArr['orderno'] ;
        $log->old_status = $logArr['old_status'] ;
        $log->new_status = $logArr['status'] ;
        $username = Yii::$app->admin->isGuest?"none":Yii::$app->admin->identity->username;
        $log->opt_name = $username ;
        $log->opt_time = date("Y-m-d H:i:s") ;
        $rs = $log->save() ;
        if(empty($rs)){
            $msg = current($log->getFirstErrors());
            return ["code"=>400,"msg"=>"失败,原因:".$msg];
        }
        return ["code"=>200,"msg"=>"成功"];
    }

    /**
     * 计算费用
     * @param $weight
     * @return int|null|\yii\db\ActiveRecord
     */
    public static function calculationCost($weight,$businessType=null){
        if(empty($weight)){
            return 0;
        }

        //如果为空默认是天虹到家
        if(empty($businessType)){
            $businessType = ConstatService::TH_TO_HOME ;
        }
        //天虹宅配
        if($businessType==ConstatService::TH_HOME_DELIVERY){
            $first_weight_price = UtilService::getSystemParamByKey("home_delivery_first_weight_price",2); //3公斤2元
        }

        //天虹到家
        if($businessType==ConstatService::TH_TO_HOME){
            $first_weight_price = UtilService::getSystemParamByKey("to_home_first_weight_price",3); //3公斤3元
        }

        $first_weight_value = UtilService::getSystemParamByKey("first_weight_value",3);

        $continue_weight_price = UtilService::getSystemParamByKey("continue_weight_price",0.5);//续重每公斤0.5元

        //不超过首重 直接返回首重的价格
        if($weight<=$first_weight_value){
            return $first_weight_price ;
        }

        $continue_weight = $weight-$first_weight_value ;
        $continue_weight = round($continue_weight) ;

        $continue_price = $continue_weight*$continue_weight_price;
        $price = $continue_price+$first_weight_price ;
        $price = round($price,2) ;
        return $price;

    }

    /**
     * 订单签收距离修改日志
     */
    public static function addSignDistancelog($logArr){
        if(Yii::$app->admin->isGuest==true){
            $username = "none" ;
            $phone = "" ;
            $user_id = 0 ;
        }else{
            $username = Yii::$app->admin->identity->username;
            $phone = Yii::$app->admin->identity->phone;
            $user_id = Yii::$app->admin->identity->user_id;
        }
        $log = new SignDistanceLog() ;
        $log->orderno = $logArr['orderno'] ;
        $log->user_id = $user_id ;
        $log->username = $username ;
        $log->phone = $phone ;
        $log->opt_time = date("Y-m-d H:i:s") ;
        $rs = $log->save() ;
        if(empty($rs)){
            $msg = current($log->getFirstErrors());
            return ["code"=>400,"msg"=>"失败,原因:".$msg];
        }
        return ["code"=>200,"msg"=>"成功"];
    }


}